<template>
  <div class="app-container">
    <el-row :gutter="20">
      <splitpanes :horizontal="this.$store.getters.device === 'mobile'" class="default-theme">
        <!--类型数据-->
        <pane size="16">
          <el-col>
            <div class="head-container">
              <el-input v-model="typeName" placeholder="请输入类型名称" clearable size="small" prefix-icon="el-icon-search" style="margin-bottom: 20px" />
            </div>
            <div class="head-container">
              <el-tree :data="carTypeOptions" :props="defaultProps" :expand-on-click-node="false" :filter-node-method="filterNode" ref="tree" node-key="id" :default-expanded-keys="[carTypeOptions[0].id]" accordion highlight-current @node-click="handleNodeClick" />
            </div>
          </el-col>
        </pane>
        <!--用户数据-->
        <pane size="84">
          <el-col>
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
              <el-form-item label="车辆类型" prop="carTypeName">
                <el-input
                  v-model="queryParams.carTypeName"
                  placeholder="请输入车辆类型"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>

            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button
                  type="primary"
                  plain
                  icon="el-icon-plus"
                  size="mini"
                  @click="handleAdd"
                  v-hasPermi="['base:icon:add']"
                >新增</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="success"
                  plain
                  icon="el-icon-edit"
                  size="mini"
                  :disabled="single"
                  @click="handleUpdate"
                  v-hasPermi="['base:icon:edit']"
                >修改</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="danger"
                  plain
                  icon="el-icon-delete"
                  size="mini"
                  :disabled="multiple"
                  @click="handleDelete"
                  v-hasPermi="['base:icon:remove']"
                >删除</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="warning"
                  plain
                  icon="el-icon-download"
                  size="mini"
                  @click="handleExport"
                  v-hasPermi="['base:icon:export']"
                >导出</el-button>
              </el-col>
              <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>

            <el-table v-loading="loading" :data="iconList" @selection-change="handleSelectionChange">
              <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="车辆类型" align="center" prop="carTypeName" width="180" :show-overflow-tooltip="true" />
              <el-table-column label="默认图标" align="center" prop="defaultIcon" width="80">
                <template slot-scope="scope">
                  <div v-if="scope.row.defaultIcon==null">暂无</div>
                  <div v-else>
                    <image-preview :src="scope.row.defaultIcon" :width="p_width" :height="p_height"/>
                  </div>
                </template>
              </el-table-column>
              <el-table-column label="工作图标" align="center" prop="workingIcon" width="80">
                <template slot-scope="scope">
                  <div v-if="scope.row.workingIcon==null">暂无</div>
                  <div v-else>
                    <image-preview :src="scope.row.workingIcon" :width="p_width" :height="p_height"/>
                  </div>
                </template>
              </el-table-column>
              <el-table-column label="等待图标" align="center" prop="loadingIcon" width="80">
                <template slot-scope="scope">
                  <div v-if="scope.row.loadingIcon==null">暂无</div>
                  <div v-else>
                    <image-preview :src="scope.row.loadingIcon" :width="p_width" :height="p_height"/>
                  </div>
                </template>
              </el-table-column>
              <el-table-column label="告警图标" align="center" prop="waringIcon" width="80">
                <template slot-scope="scope">
                  <div v-if="scope.row.waringIcon==null">暂无</div>
                  <div v-else>
                    <image-preview :src="scope.row.waringIcon" :width="p_width" :height="p_height"/>
                  </div>
                </template>
              </el-table-column>
              <el-table-column label="应急工作图标" align="center" prop="emergencyWorkingIcon" width="80">
                <template slot-scope="scope">
                  <div v-if="scope.row.emergencyWorkingIcon==null">暂无</div>
                  <div v-else>
                    <image-preview :src="scope.row.emergencyWorkingIcon" :width="p_width" :height="p_height"/>
                  </div>
                </template>
              </el-table-column>
              <el-table-column label="应急待机图标" align="center" prop="emergencyLoadingIcon" width="80">
                <template slot-scope="scope">
                  <div v-if="scope.row.emergencyLoadingIcon==null">暂无</div>
                  <div v-else>
                    <image-preview :src="scope.row.emergencyLoadingIcon" :width="p_width" :height="p_height"/>
                  </div>
                </template>
              </el-table-column>
              <el-table-column label="应急告警图标" align="center" prop="emergencyWaringIcon" width="80">
                <template slot-scope="scope">
                  <div v-if="scope.row.emergencyWaringIcon==null">暂无</div>
                  <div v-else>
                    <image-preview :src="scope.row.emergencyWaringIcon" :width="p_width" :height="p_height"/>
                  </div>
                </template>
              </el-table-column>
              <el-table-column label="app工作图标" align="center" prop="appWorkingIcon" width="80">
                <template slot-scope="scope">
                  <div v-if="scope.row.appWorkingIcon==null">暂无</div>
                  <div v-else>
                    <image-preview :src="scope.row.appWorkingIcon" :width="p_width" :height="p_height"/>
                  </div>
                </template>
              </el-table-column>
              <el-table-column label="app等待图标" align="center" prop="appLoadingIcon" width="80">
                <template slot-scope="scope">
                  <div v-if="scope.row.appLoadingIcon==null">暂无</div>
                  <div v-else>
                    <image-preview :src="scope.row.appLoadingIcon" :width="p_width" :height="p_height"/>
                  </div>
                </template>
              </el-table-column>
              <el-table-column label="app告警图标" align="center" prop="appWaringIcon" width="80">
                <template slot-scope="scope">
                  <div v-if="scope.row.appWaringIcon==null">暂无</div>
                  <div v-else>
                    <image-preview :src="scope.row.appWaringIcon" :width="p_width" :height="p_height"/>
                  </div>
                </template>
              </el-table-column>
              <el-table-column label="app应急工作图标" align="center" prop="appEmergencyWorkingIcon" width="80">
                <template slot-scope="scope">
                  <div v-if="scope.row.appEmergencyWorkingIcon==null">暂无</div>
                  <div v-else>
                    <image-preview :src="scope.row.appEmergencyWorkingIcon" :width="p_width" :height="p_height"/>
                  </div>
                </template>
              </el-table-column>
              <el-table-column label="app应急待机图标" align="center" prop="appEmergencyLoadingIcon" width="80">
                <template slot-scope="scope">
                  <div v-if="scope.row.appEmergencyLoadingIcon==null">暂无</div>
                  <div v-else>
                    <image-preview :src="scope.row.appEmergencyLoadingIcon" :width="p_width" :height="p_height"/>
                  </div>
                </template>
              </el-table-column>
              <el-table-column label="app应急告警图标" align="center" prop="appEmergencyWaringIcon" width="80">
                <template slot-scope="scope">
                  <div v-if="scope.row.appEmergencyWaringIcon==null">暂无</div>
                  <div v-else>
                    <image-preview :src="scope.row.appEmergencyWaringIcon" :width="p_width" :height="p_height"/>
                  </div>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"
                    v-hasPermi="['base:icon:edit']"
                  >修改</el-button>
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleDelete(scope.row)"
                    v-hasPermi="['base:icon:remove']"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <pagination
              v-show="total>0"
              :total="total"
              :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize"
              @pagination="getList"
            />
          </el-col>
        </pane>
      </splitpanes>
    </el-row>


    <!-- 添加或修改车辆图标对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="车辆类型" prop="carType">
          <el-cascader
            ref="cascader"
            v-model="form.carType"
            :options="carTypeOptions"
            :props="{ value: 'id', label: 'label' }"
            @change="carTypeHandleChange"></el-cascader>
        </el-form-item>
        <el-form-item label="默认图标" prop="defaultIcon">
          <image-upload-qi-niu v-model="form.defaultIcon" :limit="1" :fileSize="5" :fileType="fileType"/>
        </el-form-item>
        <el-form-item label="工作图标" prop="workingIcon">
          <image-upload-qi-niu v-model="form.workingIcon" :limit="1" :fileSize="5" :fileType="fileType" />
        </el-form-item>
        <el-form-item label="等待图标" prop="loadingIcon">
          <image-upload-qi-niu v-model="form.loadingIcon" :limit="1" :fileSize="5" :fileType="fileType"/>
        </el-form-item>
        <el-form-item label="告警图标" prop="waringIcon">
          <image-upload-qi-niu v-model="form.waringIcon" :limit="1" :fileSize="5" :fileType="fileType"/>
        </el-form-item>
        <el-form-item label="应急工作图标" prop="emergencyWorkingIcon">
          <image-upload-qi-niu v-model="form.emergencyWorkingIcon" :limit="1" :fileSize="5" :fileType="fileType"/>
        </el-form-item>
        <el-form-item label="应急待机图标" prop="emergencyLoadingIcon">
          <image-upload-qi-niu v-model="form.emergencyLoadingIcon" :limit="1" :fileSize="5" :fileType="fileType"/>
        </el-form-item>
        <el-form-item label="应急告警图标" prop="emergencyWaringIcon">
          <image-upload-qi-niu v-model="form.emergencyWaringIcon" :limit="1" :fileSize="5" :fileType="fileType"/>
        </el-form-item>
        <el-form-item label="app工作图标" prop="appWorkingIcon">
          <image-upload-qi-niu v-model="form.appWorkingIcon" :limit="1" :fileSize="5" :fileType="fileType"/>
        </el-form-item>
        <el-form-item label="app等待图标" prop="appLoadingIcon">
          <image-upload-qi-niu v-model="form.appLoadingIcon" :limit="1" :fileSize="5" :fileType="fileType"/>
        </el-form-item>
        <el-form-item label="app告警图标" prop="appWaringIcon">
          <image-upload-qi-niu v-model="form.appWaringIcon" :limit="1" :fileSize="5" :fileType="fileType"/>
        </el-form-item>
        <el-form-item label="app应急工作图标" prop="appEmergencyWorkingIcon">
          <image-upload-qi-niu v-model="form.appEmergencyWorkingIcon" :limit="1" :fileSize="5" :fileType="fileType"/>
        </el-form-item>
        <el-form-item label="app应急待机图标" prop="appEmergencyLoadingIcon">
          <image-upload-qi-niu v-model="form.appEmergencyLoadingIcon" :limit="1" :fileSize="5" :fileType="fileType"/>
        </el-form-item>
        <el-form-item label="app应急告警图标" prop="appEmergencyWaringIcon">
          <image-upload-qi-niu v-model="form.appEmergencyWaringIcon" :limit="1" :fileSize="5" :fileType="fileType"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listIcon, getIcon, delIcon, addIcon, updateIcon } from "@/api/base/icon";
import { carTypeTreeSelect } from "@/api/base/car";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { Splitpanes, Pane } from "splitpanes";
import "splitpanes/dist/splitpanes.css";

export default {
  name: "Icon",
  components: { Treeselect, Splitpanes, Pane },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆图标表格数据
      iconList: [],
      // 弹出层标题
      title: "",
      // 图片宽度
      p_width: 40,
      // 图片高度
      p_height: 40,
      // 文件类型限制
      fileType: ["png", "jpg", "jpeg"],
      // 类型名称
      typeName: undefined,
      carTypeOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carType: null,
        carTypeName: null,
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
        carType: [
          { required: true, message: "车辆类型不能为空", trigger: "change" }
        ],
      }
    };
  },
  watch: {
    // 根据名称筛选组织树
    typeName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getCarTypeTree();
  },
  methods: {
    /** 查询车辆图标列表 */
    getList() {
      this.loading = true;
      listIcon(this.queryParams).then(response => {
        this.iconList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询类型下拉树结构 */
    getCarTypeTree() {
      carTypeTreeSelect().then(response => {
        console.log(response.data);
        this.carTypeOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.carType = data.id;
      this.handleQuery();
    },
    carTypeHandleChange(e){
      console.log(e);
      let nodesInfo = this.$refs['cascader'].getCheckedNodes();
      this.form.carTypeName = nodesInfo[0].pathLabels.join('/');
      this.form.carType = nodesInfo[0].value;
    },
    // handleDefaultIconSuccess(url) {
    //   this.form.defaultIcon = url
    // },
    // handleWorkingIconSuccess(url) {
    //   this.form.workingIcon = url
    // },
    // handleLoadingIconSuccess(url) {
    //   this.form.loadingIcon = url
    // },
    // handleWaringIconSuccess(url) {
    //   this.form.waringIcon = url
    // },
    // handleEmergencyWorkingIconSuccess(url) {
    //   this.form.emergencyWorkingIcon = url
    // },
    // handleEmergencyLoadingIconSuccess(url) {
    //   this.form.emergencyLoadingIcon = url
    // },
    // handleEmergencyWaringIconSuccess(url) {
    //   this.form.emergencyWaringIcon = url
    // },
    // handleAppWorkingIconSuccess(url) {
    //   this.form.appWorkingIcon = url
    // },
    // handleAppLoadingIconSuccess(url) {
    //   this.form.appLoadingIcon = url
    // },
    // handleAppWaringIconSuccess(url) {
    //   this.form.appWaringIcon = url
    // },
    // handleAppEmergencyWorkingIconSuccess(url) {
    //   this.form.appEmergencyWorkingIcon = url
    // },
    // handleAppEmergencyLoadingIconSuccess(url) {
    //   this.form.appEmergencyLoadingIcon = url
    // },
    // handleAppEmergencyWaringIconSuccess(url) {
    //   this.form.appEmergencyWaringIcon = url
    // },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        iconId: null,
        carType: null,
        carTypeName: null,
        defaultIcon: null,
        workingIcon: null,
        loadingIcon: null,
        waringIcon: null,
        emergencyWorkingIcon: null,
        emergencyLoadingIcon: null,
        emergencyWaringIcon: null,
        appWorkingIcon: null,
        appLoadingIcon: null,
        appWaringIcon: null,
        appEmergencyWorkingIcon: null,
        appEmergencyLoadingIcon: null,
        appEmergencyWaringIcon: null,
        isCharge: null,
        useType: null,
        delFlag: null,
        createTime: null,
        updateTime: null
      };
      this.previewDefaultIcon = '';
      this.previewWorkingIcon = '';
      this.previewLoadingIcon = '';
      this.previewWaringIcon = '';
      this.previewEmergencyWorkingIcon = '';
      this.previewEmergencyLoadingIcon = '';
      this.previewEmergencyWaringIcon = '';
      this.previewAppWorkingIcon = '';
      this.previewAppLoadingIcon = '';
      this.previewAppWaringIcon = '';
      this.previewAppEmergencyWorkingIcon = '';
      this.previewAppEmergencyLoadingIcon = '';
      this.previewAppEmergencyWaringIcon = '';

      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.iconId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆图标";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const iconId = row.iconId || this.ids
      getIcon(iconId).then(response => {
        this.form = response.data;
        this.form.defaultIcon!= null?(this.previewDefaultIcon = process.env.VUE_APP_BASE_API + this.form.defaultIcon):'';
        this.form.workingIcon!= null?(this.previewWorkingIcon = process.env.VUE_APP_BASE_API + this.form.workingIcon):'';
        this.form.loadingIcon!= null?(this.previewLoadingIcon = process.env.VUE_APP_BASE_API + this.form.loadingIcon):'';
        this.form.waringIcon!= null?(this.previewWaringIcon = process.env.VUE_APP_BASE_API + this.form.waringIcon):'';
        this.form.emergencyWorkingIcon!= null?(this.previewEmergencyWorkingIcon = process.env.VUE_APP_BASE_API + this.form.emergencyWorkingIcon):'';
        this.form.emergencyLoadingIcon!= null?(this.previewEmergencyLoadingIcon = process.env.VUE_APP_BASE_API + this.form.emergencyLoadingIcon):'';
        this.form.emergencyWaringIcon!= null?(this.previewEmergencyWaringIcon = process.env.VUE_APP_BASE_API + this.form.emergencyWaringIcon):'';
        this.form.appWorkingIcon!= null?(this.previewAppWorkingIcon = process.env.VUE_APP_BASE_API + this.form.appWorkingIcon):'';
        this.form.appLoadingIcon!= null?(this.previewAppLoadingIcon = process.env.VUE_APP_BASE_API + this.form.appLoadingIcon):'';
        this.form.appWaringIcon!= null?(this.previewAppWaringIcon = process.env.VUE_APP_BASE_API + this.form.appWaringIcon):'';
        this.form.appEmergencyWorkingIcon!= null?(this.previewAppEmergencyWorkingIcon = process.env.VUE_APP_BASE_API + this.form.appEmergencyWorkingIcon):'';
        this.form.appEmergencyLoadingIcon!= null?(this.previewAppEmergencyLoadingIcon = process.env.VUE_APP_BASE_API + this.form.appEmergencyLoadingIcon):'';
        this.form.appEmergencyWaringIcon!= null?(this.previewAppEmergencyWaringIcon = process.env.VUE_APP_BASE_API + this.form.appEmergencyWaringIcon):'';
        this.open = true;
        this.title = "修改车辆图标";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.iconId != null) {
            updateIcon(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addIcon(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const iconIds = row.iconId || this.ids;
      this.$modal.confirm('是否确认删除车辆图标编号为"' + iconIds + '"的数据项？').then(function() {
        return delIcon(iconIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('base/icon/export', {
        ...this.queryParams
      }, `icon_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
